package com.kvn.dubbo.wrapper;

import com.alibaba.dubbo.common.bytecode.Wrapper;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by wangzhiyuan on 2018/8/15
 */
public class WrapperTest {
    public static void main(String[] args) {
        Point point = new Point(1, 3);
        Foo foo = new Foo(123, "xxx", point);

        Wrapper wrapper = Wrapper.getWrapper(Foo.class);
        Object i = wrapper.getPropertyValue(foo, "i");
        System.out.println("i = [" + i + "]");

        // 需要有标准的 getter 和 setter，否则报错
        Object str = wrapper.getPropertyValue(foo, "str");
        System.out.println("str = [" + str + "]");

    }

    /**
     * 将.class文件输出到d:/
     *
     * @param className 类全限定名。如：com.cn.zsy.Main
     * @param bytes      字节码数组
     */
    public static void output2File(String className, byte[] bytes) {
        String fileName = className.substring(className.lastIndexOf(".") + 1);
        String dest = "d:/" + fileName + ".class";
        System.out.println("className = [" + className + "], dest : " + dest);

        try (FileChannel channel = new FileOutputStream(dest).getChannel()) {
            channel.write(ByteBuffer.wrap(bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


